package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.chaosbean.order.bo.ShipBO;
import com.cskaoyan.bean.chaosbean.order.vo.ChannelVO;
import com.cskaoyan.bean.chaosbean.order.vo.DetailVO;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.chaosbean.vxorder.vo.ListVO;
import com.cskaoyan.bean.chaosbean.vxorder.vo.VXDetailVO;
import com.cskaoyan.mapper.MarketChannelMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.adminservice.OrderService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 楚夏三分
 * @datetime 2100/12/28 19:01
 * @description: TODO:
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;
    @Autowired
    MarketChannelMapper channelMapper;


    /**
     * TODO: 返回Order订单列表
     *
     * @param info
     * @param start            支付时间, 项目二暂未考虑此功能
     * @param end              支付时间, 项目二暂未考虑此功能
     * @param orderSn
     * @param userId
     * @param orderStatusArray
     * @return CommonData
     */
    @Override
    public CommonData<MarketOrder> list(BasePageInfo info, Date start, Date end, String orderSn, String userId, Short[] orderStatusArray) {
        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        // 条件查询
        MarketOrderExample example = new MarketOrderExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketOrderExample.Criteria criteria = example.createCriteria();
        if (start != null || end != null) {
            criteria.andPayTimeBetween(start, end);
            System.out.println("你今天真可爱!");
        }
        if (!StringUtil.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (!StringUtil.isEmpty(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        if (orderStatusArray != null) {
            List<Short> shortList = new ArrayList<>();
            for (Short aShort : orderStatusArray) {
                shortList.add(aShort);
            }
            criteria.andOrderStatusIn(shortList);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketOrder> list = orderMapper.selectByExample(example);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * TODO: 根据订单id查询详细信息
     *
     * @param id 订单id
     * @return user \ order \ orderGoods
     */
    @Transactional
    @Override
    public DetailVO detail(Integer id) {
        // 查询 order \ user
        MarketOrder marketOrder = orderMapper.selectByPrimaryKey(id);
        Integer userId = marketOrder.getUserId();
        MarketUser marketUser = userMapper.selectByPrimaryKey(userId);

        // 根据orderId查询orderGoods
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(marketOrderGoodsExample);

        // 封装进DetailVO
        DetailVO detailVO = new DetailVO();
        detailVO.setOrder(marketOrder);
        detailVO.setOrderGoods(marketOrderGoods);
        detailVO.setUser(marketUser);

        return detailVO;
    }

    @Override
    public void ship(ShipBO shipBO) {
        orderMapper.updateByShipBO(shipBO);
    }

    /**
     * TODO: 获得所有快递公司code与name
     *
     * @return
     */
    @Override
    public List<ChannelVO> channel() {
        List<ChannelVO> channelVOList = channelMapper.selectChannel();
        return channelVOList;
    }

    @Override
    public void deleteOrder(Integer orderId) {
        orderMapper.deleteByPrimaryKeyLogical(orderId);
    }

    /**
     * TODO: 退款操作
     *
     * @param orderId     退款订单id
     * @param refundMoney 退款金额
     */
    @Override
    public void refund(Integer orderId, Double refundMoney) {
        MarketOrder<Object> marketOrder = new MarketOrder<>();
        marketOrder.setId(orderId);
        BigDecimal bigDecimal = new BigDecimal(refundMoney);
        marketOrder.setRefundAmount(bigDecimal);
        orderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    /**
     * TODO:略长 谨慎点开
     *
     * @param info     分页
     * @param showType 用户的ID
     * @return 返回VXOrder订单列表
     */
    @Override
    public ListVO listOrder(BasePageInfo info, Integer showType) {
        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());

        ListVO listVo = new ListVO();
        List<ListVO.ListBean> outer = null;
        if (showType == 0) {
            outer = orderMapper.selectById(1);
        } else if (showType == 1) {//待付款页面 101
            showType = 101;
            outer = orderMapper.selectByIdAndSta(1, showType);
        } else if (showType == 2) {//代发货页面 201
            showType = 201;
            outer = orderMapper.selectByIdAndSta(1, showType);
        } else if (showType == 3) {//待收货页面 301
            showType = 301;
            outer = orderMapper.selectByIdAndSta(1, showType);
        } else if (showType == 4) {//待评价页面 401
            showType = 401;
            outer = orderMapper.selectByIdAndSta(1, showType);
        }

        for (int i = 0; i < outer.size(); i++) {
            if (outer.get(i).getOrderStatusText().equals("101")) {
                outer.get(i).setOrderStatusText("未付款");
                outer.get(i).setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, false, false, false, false));
            } else if (outer.get(i).getOrderStatusText().equals("102")) {
                outer.get(i).setOrderStatusText("用户取消,退款中");
                outer.get(i).setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, false, false, false, false));
            } else if (outer.get(i).getOrderStatusText().equals("103")) {
                outer.get(i).setOrderStatusText("系统取消");
                outer.get(i).setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, false, false, false, false));
            } else if (outer.get(i).getOrderStatusText().equals("201")) {
                outer.get(i).setOrderStatusText("已付款");
                outer.get(i).setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, false, true, false, false));
            } else if (outer.get(i).getOrderStatusText().equals("202")) {
                outer.get(i).setOrderStatusText("申请退款");
                outer.get(i).setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, false, false, false, false));
            } else if (outer.get(i).getOrderStatusText().equals("203")) {
                outer.get(i).setOrderStatusText("已退款");
                outer.get(i).setHandleOption(new ListVO.ListBean.HandleOptionBean(false, true, false, false, false, false, false, false));
            } else if (outer.get(i).getOrderStatusText().equals("301")) {
                outer.get(i).setOrderStatusText("已发货");
                outer.get(i).setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, true, false, false, false));
            } else if (outer.get(i).getOrderStatusText().equals("401")) {
                outer.get(i).setOrderStatusText("用户收货");
                outer.get(i).setHandleOption(new ListVO.ListBean.HandleOptionBean(false, true, false, true, false, false, true, true));
            } else if (outer.get(i).getOrderStatusText().equals("402")) {
                outer.get(i).setOrderStatusText("系统收货");
                outer.get(i).setHandleOption(new ListVO.ListBean.HandleOptionBean(false, true, false, true, false, false, true, true));
            }
            //老师要求写死就行
            outer.get(i).setIsGroupin(false);
            outer.get(i).setGoodsList(orderMapper.selectOrderGoodsById(outer.get(i).getOrderSn()));
        }


        listVo.setList(outer);
        PageInfo<ListVO.ListBean> pageInfo = new PageInfo<>(outer);
        listVo.setLimit(pageInfo.getPageSize());//limit
        listVo.setPage(pageInfo.getPageNum());//page
        listVo.setPages(pageInfo.getPages());//pages
        listVo.setTotal((int) pageInfo.getTotal());//total


        return listVo;
    }

    /**
     * TODO：略长 谨慎点开
     *
     * @param orderId 订单的Id
     * @return 订单商品详情页面
     */
    @Override
    public VXDetailVO vxOrderDetail(Integer orderId) {
        VXDetailVO vo = new VXDetailVO();
        String[] Ex = orderMapper.selectChannelById(orderId);
        MarketOrderGoodsExample example = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> goodsList = orderGoodsMapper.selectByExample(example);
        MarketOrder marketOrder = orderMapper.selectByPrimaryKey(orderId);
        if (((Integer) marketOrder.getOrderStatus()) == 101) {
            marketOrder.setOrderStatus("未付款");
            marketOrder.setHandleOption(new ListVO.ListBean.HandleOptionBean(true, false, true, false, false, false, false, false));
        } else if (((Integer) marketOrder.getOrderStatus()) == 102) {
            marketOrder.setOrderStatus("用户取消退款中");
            marketOrder.setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, false, false, false, false));
        } else if (((Integer) marketOrder.getOrderStatus()) == 201) {
            marketOrder.setOrderStatus("已付款");
            marketOrder.setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, false, true, false, false));
        } else if (((Integer) marketOrder.getOrderStatus()) == 202) {
            marketOrder.setOrderStatus("申请退款");
            marketOrder.setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, false, false, false, false));
        } else if (((Integer) marketOrder.getOrderStatus()) == 203) {
            marketOrder.setOrderStatus("已退款");
            marketOrder.setHandleOption(new ListVO.ListBean.HandleOptionBean(false, true, false, false, false, false, false, false));
        } else if (((Integer) marketOrder.getOrderStatus()) == 301) {
            marketOrder.setOrderStatus("已发货");
            marketOrder.setHandleOption(new ListVO.ListBean.HandleOptionBean(false, false, false, false, true, false, false, false));
        } else if (((Integer) marketOrder.getOrderStatus()) == 401) {
            marketOrder.setOrderStatus("用户收货");
            marketOrder.setHandleOption(new ListVO.ListBean.HandleOptionBean(false, true, false, true, false, false, true, true));
        } else if (((Integer) marketOrder.getOrderStatus()) == 402) {
            marketOrder.setOrderStatus("系统收货");
            marketOrder.setHandleOption(new ListVO.ListBean.HandleOptionBean(false, true, false, true, false, false, true, true));
        }
        vo.setExpressInfo(Ex);
        vo.setOrderGoods(goodsList);
        vo.setOrderInfo(marketOrder);
        return vo;
    }

    /**
     * 用户取消订单
     *
     * @param orderId
     */
    @Override
    public void vxUpdateStatus(Integer orderId) {
        //102
        orderMapper.updateStatus(orderId);
    }

    /**
     * 用户确认收货
     *
     * @param id
     */
    @Override
    public void vxUpdateStatusConfirmById(Integer id) {
        //401
        orderMapper.updateStatusConfirmById(id);
    }

    @Override
    public MarketOrderGoods vxOrderGoodsComment(Integer oId, Integer gId) {
        Integer id = orderGoodsMapper.selectUseId(oId,gId);
        MarketOrderGoods orderGoods = orderGoodsMapper.selectByPrimaryKey(id);
        return orderGoods;
    }

    @Override
    public void vxUpdateStatusRefund(Integer id) {
        orderMapper.updateStatusRefund(id);
    }



}
